package openbang.account.web.portal.client;

import openbang.account.service.intf.AccountService;
import openbang.account.service.vo.AccountServiceEnum;
import openbang.account.service.vo.UserLoginInfo;
import openbang.account.web.portal.widget.EmailTextBox;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Document;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.PasswordTextBox;

/**
 * 登录控制
 * @author aibo zeng
 *
 */
public class Login implements EntryPoint {
	//jsp页面，需要控制的组件
	private EmailTextBox email = null;
	private PasswordTextBox pwd = null;
	private Button  login = null;
	private Button  googleLogin = null;
	
	private static String CookieUserId ="openbanguserid";
	
	private void bindUI(){
		login = Button.wrap(Document.get().getElementById("loginBtn"));
		email = EmailTextBox.wrap(Document.get().getElementById("emailTxt"));
		pwd   = PasswordTextBox.wrap(Document.get().getElementById("pwdTxt"));
		googleLogin = Button.wrap(Document.get().getElementById("toGoogleLoginBtn"));
		
	}
	
	@Override
	public void onModuleLoad() {
		bindUI();
		//Window.alert(login.getText());
		login.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				doLogin();
			}
		});

		email.addBlurHandler(new BlurHandler() {
			public void onBlur(BlurEvent event) {
				//Window.alert("onBlur");
				//Window.alert(GWT.getModuleBaseURL());
			}
		});
		email.addKeyUpHandler(new KeyUpHandler() {
			public void onKeyUp(KeyUpEvent event) {
				if(email.invalidNum()>0){
					return;
				}
				if(event.getNativeKeyCode()==KeyCodes.KEY_ENTER){
					//跳到输入密码框的位置
					pwd.setFocus(true);
				}
			}
		});
		pwd.addKeyUpHandler(new KeyUpHandler() {
			public void onKeyUp(KeyUpEvent event) {
				if(event.getNativeKeyCode()==KeyCodes.KEY_ENTER){
				    login.click();
				}
			}
		});
		googleLogin.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {				
				//获取 Google登录的链接
				AccountService.Util.getInstance().getLoginUserInfo(getLoginAfterUrl(),new AsyncCallback<UserLoginInfo>(){
					public void onFailure(Throwable caught) {
						Window.alert(caught.getMessage());
					}
					public void onSuccess(UserLoginInfo result) {
						toSwitchLoginAfterUrl(result.getLoginUrl());
					}
				});
			}
		});		
		
		//加载完，本站登录按钮才管用
		login.setEnabled(true);
		googleLogin.setEnabled(true);
		//使用cookie的登录值
		email.setText(Cookies.getCookie(CookieUserId));
	}
	
	//本站登录
	public void doLogin(){
		if(email.invalidNum()>0){
			Window.alert("邮箱地址不对,请重新录入");
			//email.setText("");
			email.setFocus(true);
			return;
		}
		if(pwd.getText()==null || pwd.getText().trim().length()<=0){
			Window.alert("请录入密码");
			pwd.setText("");
			pwd.setFocus(true);
			return;
		}
		login.setEnabled(false);
		AccountService.Util.getInstance().checkForLogin(email.getArrValue()[0], pwd.getText(), new AsyncCallback<Integer>(){
			public void onSuccess(Integer result) {				
				if(result==AccountServiceEnum.OK.getCode()){
					//本站登录后跳转的页面  由 accountLogin.jsp的JS控制
					toSwitchLoginAfterUrl(null);
					//保存cookies
					Cookies.setCookie(CookieUserId,email.getText());
				}
				else{
					Window.alert(AccountServiceEnum.getDesc(result));
					login.setEnabled(true);
				}
			}
			public void onFailure(Throwable caught) {
				Window.alert(caught.getMessage());
				login.setEnabled(true);
			}
		});
	}
	
	//获取登录后应该跳转的页面  在 JSP的 javascript中定义      google登录需要
	public static native String getLoginAfterUrl() /*-{  
         return parent.getLoginAfterDefaultUrl();
    }-*/;	
	
	//调用登录后应该跳转的页面  参数url可以为空
	public static native void toSwitchLoginAfterUrl(String url) /*-{  
         parent.toLoginAfterUrl(url);
    }-*/;	
	
}
